package com.pmpd.activity.service.background.impl;

import com.pmpd.activity.constants.ActivityConstant;
import com.pmpd.activity.controller.background.param.BgHotActivityParam;
import com.pmpd.activity.controller.background.param.BgHotActivitySelectParam;
import com.pmpd.activity.dao.HotActivityMapper;
import com.pmpd.activity.po.HotActivity;
import com.pmpd.activity.po.HotActivityExample;
import com.pmpd.activity.service.background.BgHotActivityService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 18/05/18
 * \* Description:
 * \
 */
@Service
public class BgHotActivityServiceImpl implements BgHotActivityService {

    @Resource
    private HotActivityMapper hotActivityMapper;

    @Override
    public HotActivity insert(BgHotActivityParam param) {
        HotActivity hotActivity = new HotActivity();
        hotActivity.setTitle(param.getTitle());
        hotActivity.setDescription(param.getDescription());
        if (StringUtils.isNotBlank(param.getAppId()) && StringUtils.isNumeric(param.getAppId())) {
            hotActivity.setAppId(Long.valueOf(param.getAppId()));
        }
        hotActivity.setProjectId(param.getProjectId());
        hotActivity.setUrl(param.getUrl());
        hotActivity.setBeginTime(param.getBeginTime());
        hotActivity.setEndTime(param.getEndTime());
        hotActivity.setValidity(param.getValidity());
        hotActivity.setBannerUrl(param.getBannerUrl());
        hotActivity.setUsable(param.getUsable());
        hotActivity.setReadNumber(0);
        hotActivity.setPriority(param.getPriority());
        hotActivity.setHotActivityType(param.getHotActivityType());
        hotActivity.setJumpType(param.getJumpType());
        hotActivity.setVersion(param.getVersion());
        hotActivity.setInnerJumpPage(param.getInnerJumpPage());
        hotActivity.setInnerJumpParam(param.getInnerJumpParam());
        hotActivity.setAppJumpName(param.getAppJumpName());
        hotActivity.setAppJumpParam(param.getAppJumpParam());
        if(StringUtils.isNotBlank(param.getLang())){
            hotActivity.setLang(param.getLang().toUpperCase());
        }
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            hotActivity.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        if (StringUtils.isNotBlank(param.getCreatorId()) && StringUtils.isNumeric(param.getCreatorId())) {
            hotActivity.setCreatorId(Long.valueOf(param.getCreatorId()));
            hotActivity.setEditorId(hotActivity.getCreatorId());
        }
        hotActivity.setCreateTime(System.currentTimeMillis() / 1000);
        hotActivity.setUpdateTime(hotActivity.getCreateTime());
        int success = hotActivityMapper.insertSelective(hotActivity);
        return (success == 1) ? hotActivity : null;
    }

    @Override
    public boolean update(BgHotActivityParam param) {
        HotActivity hotActivity = new HotActivity();
        hotActivity.setId(Long.valueOf(param.getId()));
        hotActivity.setTitle(param.getTitle());
        hotActivity.setDescription(param.getDescription());
        if (StringUtils.isNotBlank(param.getAppId()) && StringUtils.isNumeric(param.getAppId())) {
            hotActivity.setAppId(Long.valueOf(param.getAppId()));
        }
        hotActivity.setProjectId(param.getProjectId());
        hotActivity.setUrl(param.getUrl());
        hotActivity.setBeginTime(param.getBeginTime());
        hotActivity.setEndTime(param.getEndTime());
        hotActivity.setValidity(param.getValidity());
        hotActivity.setBannerUrl(param.getBannerUrl());
        hotActivity.setUsable(param.getUsable());
        hotActivity.setReadNumber(param.getReadNumber());
        hotActivity.setPriority(param.getPriority());
        hotActivity.setHotActivityType(param.getHotActivityType());
        hotActivity.setJumpType(param.getJumpType());
        hotActivity.setVersion(param.getVersion());
        hotActivity.setInnerJumpPage(param.getInnerJumpPage());
        hotActivity.setInnerJumpParam(param.getInnerJumpParam());
        hotActivity.setAppJumpName(param.getAppJumpName());
        hotActivity.setAppJumpParam(param.getAppJumpParam());
        if(StringUtils.isNotBlank(param.getLang())){
            hotActivity.setLang(param.getLang().toUpperCase());
        }
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            hotActivity.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        if (StringUtils.isNotBlank(param.getEditorId()) && StringUtils.isNumeric(param.getEditorId())) {
            hotActivity.setEditorId(Long.valueOf(param.getEditorId()));
        }
        hotActivity.setUpdateTime(System.currentTimeMillis() / 1000);
        return hotActivityMapper.updateByPrimaryKeySelective(hotActivity) == 1;
    }

    @Override
    public boolean deleteByIdList(List<Long> idList) {
        HotActivityExample example = new HotActivityExample();
        example.createCriteria().andIdIn(idList);
        int success = hotActivityMapper.deleteByExample(example);
        return (success >= 1) ? true : false;
    }

    @Override
    public HotActivity selectById(Long id) {
        return hotActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HotActivity> selectByBgHotActivitySelectParam(BgHotActivitySelectParam param) {
        HotActivityExample example = this.getBackgroundHotActivitySelectParamExample(param);
        //排序子句
        String orderByClause = ActivityConstant.DEFAULT_ORDER_BY_CLAUSE;
        if (param.getSortField() != null) {
            //根据前端所传参数不同生成对应的orderBy子句
            orderByClause = ActivityConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), ActivityConstant.UPDATE_TIME)
                    + ActivityConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), ActivityConstant.DESC);
        }
        example.setOrderByClause(orderByClause);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return hotActivityMapper.selectByExample(example);
    }

    @Override
    public int countByBgHotActivitySelectParam(BgHotActivitySelectParam param) {
        HotActivityExample example = this.getBackgroundHotActivitySelectParamExample(param);
        return hotActivityMapper.countByExample(example);
    }

    private HotActivityExample getBackgroundHotActivitySelectParamExample(BgHotActivitySelectParam param) {
        HotActivityExample example = new HotActivityExample();
        HotActivityExample.Criteria criteria = example.createCriteria();
        //如果 公司ID 不为空，则匹配 公司ID 进行查询
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())
                && !Long.valueOf(param.getCompanyId()).equals(0L)) {
            criteria.andCompanyIdEqualTo(Long.valueOf(param.getCompanyId()));
        }
        //如果 AppID 不为空，则匹配 AppID 进行查询
        if (StringUtils.isNotBlank(param.getAppId()) && StringUtils.isNumeric(param.getAppId())
                && !Long.valueOf(param.getAppId()).equals(0L)) {
            criteria.andAppIdEqualTo(Long.valueOf(param.getAppId()));
        }
        //如果 语言编号 不为空，则匹配 语言编号 进行查询
        if (StringUtils.isNotBlank(param.getLang())) {
            criteria.andLangEqualTo(param.getLang().toUpperCase());
        }
        //对 标题 进行模糊搜索
        if (StringUtils.isNotBlank(param.getSearchKeyword())) {
            criteria.andTitleLike("%" + param.getSearchKeyword() + "%");
        }
        return example;
    }

    @Override
    public int getHotActivityStatus(HotActivity param) {
        if (param == null || param.getUsable() == null
                || param.getBeginTime() == null || param.getEndTime() == null
                || param.getBeginTime() > param.getEndTime()
                || !param.getUsable().equals(ActivityConstant.ACTIVITY_USABLE)) {
            //1未上架， 2未开始， 3已结束, 4进行中
            return ActivityConstant.ACTIVITY_STATUS_UNUSABLE;
        } else if (param.getBeginTime() * 1000 > System.currentTimeMillis()) {
            return ActivityConstant.ACTIVITY_STATUS_NOT_START;
        } else if (param.getEndTime() * 1000 < System.currentTimeMillis()) {
            return ActivityConstant.ACTIVITY_STATUS_FINISHED;
        } else if (param.getBeginTime() * 1000 < System.currentTimeMillis()
                && param.getEndTime() * 1000 > System.currentTimeMillis()) {
            return ActivityConstant.ACTIVITY_STATUS_ON_GOING;
        } else {
            return ActivityConstant.ACTIVITY_STATUS_UNUSABLE;
        }
    }

}